package com.gzw.shop.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.cjj.MaterialRefreshLayout;
import com.google.gson.reflect.TypeToken;
import com.gzw.shop.Contans;
import com.gzw.shop.R;
import com.gzw.shop.adapter.BaseAdapter;
import com.gzw.shop.adapter.DividerItemDecoration;
import com.gzw.shop.adapter.HWAdapter;
import com.gzw.shop.bean.Page;
import com.gzw.shop.bean.Wares;
import com.gzw.shop.utils.Pager;
import com.gzw.shop.widget.GToolbar;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class WareListActivity extends BaseActivity implements Pager.OnPagerListener<Wares>, TabLayout.OnTabSelectedListener {

    @BindView(R.id.toolbar)
    GToolbar mToolbar;
    @BindView(R.id.tab_layout)
    TabLayout tab_layout;
    @BindView(R.id.tv_summary)
    TextView tv_summary;
    @BindView(R.id.recyclerView)
    RecyclerView mRecyclerView;
    @BindView(R.id.refresh)
    MaterialRefreshLayout mRefreshLayout;

    private HWAdapter mAdapter;

    private Context context;

    private Pager mPager;

    private int campaignId = 0;
    private int orderBy = 0;

    public static final int TAG_DEFAULT = 0;
    public static final int TAG_SALE = 1;
    public static final int TAG_PRICE = 2;

    public static final int ACTION_LIST = 1;
    public static final int ACTION_GRID = 2;

    @Override
    public int getLayoutId() {
        return R.layout.activity_ware_list;
    }

    @Override
    public void initView() {

        context = this;

        campaignId = getIntent().getIntExtra(Contans.CAMPAIGN_ID, 0);

        mToolbar.setIvLeftOnclickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        mToolbar.setIvRightIcon(R.drawable.grid);
        mToolbar.setIvRightTag(ACTION_LIST);

        mToolbar.setIvRightOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                int action = (int) view.getTag();

                if (action == ACTION_LIST) {

                    mToolbar.setIvRightIcon(R.drawable.list);
                    mToolbar.setIvRightTag(ACTION_GRID);
                    mAdapter.resetLayout(R.layout.template_grid_wares);
                    mRecyclerView.setLayoutManager(new GridLayoutManager(context, 2));
                    mRecyclerView.setAdapter(mAdapter);//注意：重新设置LayoutManager之后，一定要再设置一次Adapter

                } else if (action == ACTION_GRID) {

                    mToolbar.setIvRightIcon(R.drawable.grid);
                    mToolbar.setIvRightTag(ACTION_LIST);
                    mAdapter.resetLayout(R.layout.template_hot_wares);
                    mRecyclerView.setLayoutManager(new LinearLayoutManager(context));
                    mRecyclerView.setAdapter(mAdapter);

                }
            }
        });

        initTab();
    }

    @Override
    public void initData() {
        getData();
    }

    private void initTab() {

        TabLayout.Tab tab = tab_layout.newTab();
        tab.setText("默认");
        tab.setTag(TAG_DEFAULT);
        TabLayout.Tab tab1 = tab_layout.newTab();
        tab1.setText("价格");
        tab1.setTag(TAG_PRICE);
        TabLayout.Tab tab2 = tab_layout.newTab();
        tab2.setText("销量");
        tab2.setTag(TAG_SALE);

        tab_layout.addTab(tab);
        tab_layout.addTab(tab1);
        tab_layout.addTab(tab2);

        tab_layout.addOnTabSelectedListener(this);
    }

    private void getData() {

        mPager = Pager.newBuilder()
                .setUrl(Contans.API.WARES_CAMPAIN_LIST)
                .putParam("campaignId", campaignId)
                .putParam("orderBy", orderBy)
                .setRefreshLayout(mRefreshLayout)
                .setCanLoadMore(true)
                .setPageSize(10)
                .setOnPagerListener(this)
                .builder(this, new TypeToken<Page<Wares>>() {
                }.getType());

        mPager.request();

    }

    @Override
    public void load(final List<Wares> datas, int totalPage, int totalCount) {
        tv_summary.setText("共有" + datas.size() + "件商品");

        if (mAdapter == null) {
            mAdapter = new HWAdapter(this, datas);
            mRecyclerView.setAdapter(mAdapter);
            mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
            mRecyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST));
            mRecyclerView.setItemAnimator(new DefaultItemAnimator());

            mAdapter.setListener(new BaseAdapter.OnItemClickListener() {
                @Override
                public void OnClick(View view, int position) {
                    Intent intent = new Intent(WareListActivity.this, WareDetailActivity.class);
                    intent.putExtra(Contans.WARES, datas.get(position));
                    startActivity(intent);
                }
            });

        } else {
            mAdapter.refreshData(datas);
        }
    }

    @Override
    public void refresh(List<Wares> datas, int totalPage, int totalCount) {

        mAdapter.refreshData(datas);
        mRecyclerView.scrollToPosition(0);
    }

    @Override
    public void loadMore(List<Wares> datas, int totalPage, int totalCount) {

        mAdapter.loadMoreData(datas);
    }

    @Override
    public void onTabSelected(TabLayout.Tab tab) {

        orderBy = (int) tab.getTag();
        mPager.putParam("orderBy", orderBy);
        mPager.request();
    }

    @Override
    public void onTabUnselected(TabLayout.Tab tab) {

    }

    @Override
    public void onTabReselected(TabLayout.Tab tab) {

    }
}
